/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ipparser;

import cn.ac.iie.di.tools.Util;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author alexmu
 */
public class IPGeoLocatorInit {

    static Logger logger = null;
//
//    static {
//        PropertyConfigurator.configure("log4j.properties");
//        logger = Logger.getLogger(IPGeoLocatorInit.class.getName());
//    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
//        String configurationFileName = "cls-etl.properties";
//        Configuration conf = Configuration.getConfiguration(configurationFileName);
//        if (conf == null) {
//            throw new Exception("reading " + configurationFileName + " is failed.");
//        }
//        
//        logger.info("initializng runtime enviroment...");
//        if (!RuntimeEnv.initialize(conf)) {
//            throw new Exception("initializng runtime enviroment is failed");
//        }
//        logger.info("initialize runtime enviroment successfully");
//        
        RangeSearch ipGeoLocator = new RangeSearch();
//        ResultSet rs = null;
//        try {
//            String sql = "select start_ip_n,end_ip_n,country,district,isp from dic_ip_location";
//            while (true) {
//                try {
//                    rs = DaoPool.getDao(RuntimeEnv.METADB_CLUSTER).executeQuery(sql);
//                    break;
//                } catch (Exception ex) {
//                    continue;
//                }
//            }
        int num = 0;
        File file = new File(ClassLoader.getSystemClassLoader().getResource("ip.merge.txt").getFile());
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] id2Province = line.split("[|]");
            ipGeoLocator.append(Util.ip2long(id2Province[0]), Util.ip2long(id2Province[1]), "country", id2Province[2]);
            ipGeoLocator.append(Util.ip2long(id2Province[0]), Util.ip2long(id2Province[1]), "district", id2Province[4] + id2Province[5]);
            ipGeoLocator.append(Util.ip2long(id2Province[0]), Util.ip2long(id2Province[1]), "isp", id2Province[6]);
            num++;
            System.out.println(num);
        }

//        logger.info("init ipGeoLocator successfully with " + num + " records");
        ipGeoLocator.contructArray();

        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("IPGeoLocator.dat")));
        out.writeObject(ipGeoLocator);
        out.close();

    }

}
